package com.hbnu.system.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.PatentfileMapper;
import com.hbnu.system.model.entity.Patent;
import com.hbnu.system.model.entity.Patentfile;
import com.hbnu.system.service.IPatentService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.service.PatentfileService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.BindException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * (Patentfile)表服务实现类
 *
 * @author makejava
 * @since 2023-10-14 16:52:17
 */
@Service("patentfileService")
public class PatentfileServiceImpl extends ServiceImpl<PatentfileMapper, Patentfile> implements PatentfileService {
    @Autowired
    FileUploadUtil fileUploadUtil;

    @Autowired
    IPatentService iPatentService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private CheckRoleUtil checkRoleUtil;

    /**
     * 新增数据
     * @param patentfile
     * @return
     */
    @Override
    public boolean addPatentfile(Patentfile patentfile) {
        //拦截普通用户
        Patent patent = iPatentService.getById(patentfile.getPatentid());
        checkRoleUtil.checkPermissionAchievement(patent.getStatus());

        patentfile.setFiledate(LocalDateTime.now());
        patentfile.setFlag(1);
        if(baseMapper.insert(patentfile) != 1)
            throw new BaseException("上传失败,请重新尝试");
        return true;
    }

    @Override
    public Patentfile getPatentfile(String id) {
        Patentfile patentfile = super.getOne(Wrappers.lambdaQuery(Patentfile.class)
                .eq(Patentfile::getFileid,id));
        if(Objects.isNull(patentfile)){
            throw new BaseException("文件编号异常!未查找到该文件信息");
        }
        return patentfile;
    }

    @Override
    public List<Patentfile> pagePatentfile(String id) {
        if(StrUtil.isBlank(id)){
            throw new BaseException("专利编号为空值");
        }
        List<Patentfile> patentfileList = baseMapper.selectList(Wrappers.lambdaQuery(Patentfile.class)
                .eq(Patentfile::getPatentid,id));
        return patentfileList;
    }

    @Override
    public boolean deletPatentfile(String id) throws IOException {
        Patentfile one = super.getOne(Wrappers.lambdaQuery(Patentfile.class)
                .eq(Patentfile::getFileid, id));
        if (Objects.isNull(one)){
            throw new BaseException("文件编号有误!");
        }
        Patent patent = iPatentService.getById(one.getPatentid());
        checkRoleUtil.checkPermissionAchievement(patent.getStatus());

        boolean delete = fileUploadUtil.delete(one.getFileurl());
        if(!delete){
            throw new BindException("附件删除失败");
        }

        return baseMapper.deleteById(id) == 1;
    }



}
